﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SceneViewLoader
{
    public class OgreConfig
    {
        private StreamWriter outfile;
        private StreamReader infile;

        public string RenderSystem;
        public string FSAA;
        public string FullScreen;
        public string VSync;
        public string VideoMode;

        public OgreConfig()
        {
            toDefaultConfig();
        }

        public void loadConfig()
        {
            try
            {
                infile = new StreamReader("ogre.cfg");
                RenderSystem = infile.ReadLine();
                infile.ReadLine();
                infile.ReadLine();
                infile.ReadLine();
                FSAA = infile.ReadLine();
                infile.ReadLine();
                FullScreen = infile.ReadLine();
                infile.ReadLine();
                VSync = infile.ReadLine();
                infile.ReadLine();
                VideoMode = infile.ReadLine();
                infile.Close();
            }
            catch
            {
                MessageBox.Show("No config file found! Returning to defaults.", "Error");
                toDefaultConfig();
            }
        }

        public void writeConfig()
        {
            try
            {
                outfile = new StreamWriter("ogre.cfg");

                outfile.WriteLine(RenderSystem);
                outfile.WriteLine("");
                outfile.WriteLine("[Direct3D9 Rendering Subsystem]");
                outfile.WriteLine("Allow NVPerfHUD=No");
                outfile.WriteLine(FSAA);
                outfile.WriteLine("Floating-point mode=Fastest");
                outfile.WriteLine(FullScreen);
                outfile.WriteLine("Resource Creation Policy=Create on all devices");
                outfile.WriteLine(VSync);
                outfile.WriteLine("VSync Interval=1");
                outfile.WriteLine(VideoMode);
                outfile.WriteLine("sRGB Gamma Conversion=No");
                outfile.WriteLine("");
                outfile.WriteLine("[OpenGL Rendering Subsystem]");
                if (VideoMode.Contains("@ 32-bit colour"))
                {
                    outfile.WriteLine("Colour Depth=32");
                }
                else
                {
                    outfile.WriteLine("Colour Depth=16");
                }
                outfile.WriteLine("Display Frequency=N/A");
                outfile.WriteLine(FSAA);
                outfile.WriteLine(FullScreen);
                outfile.WriteLine("RTT Preferred Mode=FBO");
                outfile.WriteLine(VSync);
                outfile.WriteLine("VSync Interval=1");
                outfile.WriteLine(VideoMode.Replace(" @ 16-bit colour", "").Replace(" @ 32-bit colour", ""));
                outfile.WriteLine("sRGB Gamma Conversion=No");
                outfile.Close();
            }
            catch
            {
                MessageBox.Show("Cannot write config file!", "Error");
            }
        }

        public void toDefaultConfig()
        {
             RenderSystem = "Render System=Direct3D9 Rendering Subsystem";
             FSAA = "FSAA=0";
             FullScreen = "Full Screen=No";
             VSync = "VSync=No";
             VideoMode = "Video Mode=1366 x 768 @ 16-bit colour";
        }

        public void writeDefaultConfig()
        {
            toDefaultConfig();
            writeConfig();
        }
    }
}
